var tmpObj;
var sign="0";
$(document).ready(function() {	
	findAllDeptType2();

		// 指派部门窗体的定义
	$("#deptChooseWindow1").dialog({
				position: 'top' ,
				autoOpen : false,
				minWidth : 700,
				modal : true,
				title : '选择对象',
				buttons : {
					'确定' : function() {
						//alert($("#selDepts option").get(0).value);
		               if(sign!="1"){
						tmpObj.prev().prev().text($("#selDepts1 option").get(0).text);
						tmpObj.prev().text($("#selDepts1 option").get(0).value);
						tmpObj.val("修改");
		                $(this).dialog('close');
		            	   }
		               else{
		               var lenthk = $("#selDepts1 option").length;
						var str = "";
						var i = 0;
						$("#selDepts1 option").each(function() {
							i++;
						   if(i==lenthk){str += $(this).text();}
						   else{
							   str += $(this).text() + ",";
						   }
						});
						var i=0;
//						alert(str);
						sign=0;
						tmpObj.prev().prev().text(str);
						tmpObj.val("修改");
                         $(this).dialog('close');
		               }
//						window.location.href="/ag/wzp/event_man.jsp";
//						var str = "";
//						$("#selDepts option").each(function() {
//									str += $(this).text() + ",";
//								});
//						$("#taskDept").val(str);
//						$(this).dialog('close');
						
					},
					'取消.' : function() {
						$("#selDepts1").empty();
						$(this).dialog('close');
					}
				},
				close : function() {
					$("#selDepts1").empty();
					return false;
				}
			});

	$('.sltDept').click(function() {
			//$("#deptChooseWindow").empty();
			//$("#subDepts").empty();
			//$("#selDepts").empty();
			//$("#subDepts").get(0).selectedIndex=-1; 
			//$("#selDepts").get(0).selectedIndex=-1;
			//alert("dfgfdgdgd");	
			$("#subDepts").empty();
			findAllDeptType2();
			tmpObj=$(this);	
			$("#deptChooseWindow1").dialog("open");
		});
				/**
				 * 建议实施应急预案部门的class
				 */
	$('.ssya').click(function() {//让ssya只能单选
		sign = "1";
		$("#subDepts").empty();
		findAllDeptType2();
		tmpObj = $(this);
		$("#deptChooseWindow1").dialog("open");
	});
	
	//应该没有，先注销了。
//	// 弹出任务部门指派的选择窗体
//	$('.slt').click(function() {
//		//$("#deptChooseWindow").empty();
//		//$("#subDepts").empty();
//		//$("#selDepts").empty();
//		//$("#subDepts").get(0).selectedIndex=-1; 
//		//$("#selDepts").get(0).selectedIndex=-1;
//		$("#subDepts").empty();
//		findAllDeptType2();
//		tmpObj=$(this);	
//		$("#deptChooseWindow").dialog("open");
//	});
	
	// 弹出任务部门指派的选择窗体
	
	$("#selectUser").click(function() {
		 $("#selectArea").css("display","block");
	});
	

	
	// 指派部门的窗体上两个按钮的响应函数 addDept 和 delDept
	$("#addDept1").click(function() {
		if ($("#subDepts1 option:selected").length > 0) {
			$("#subDepts1 option:selected").each(function() {
				var strHtml="";
				var id="",name=""
				var udId = $(this).val();
				var udName = $(this).text();
				
				if(sign=="1"){}
				else{
				if($("#selDepts1 option").length == 1){
					//alert("只能选择一个人！");
					return ;
				}}
				
				if ($("#selDepts1 option").length > 0 ) {
					var t = true;
					$("#selDepts1 option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					})
					if(t){
						id=udId;
						name = udName
						strHtml+="<option class='class1' name='selDepts1' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					id=udId;
					name = udName
					strHtml+="<option class='class1' name='selDepts1' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				$("#selDepts1").append(strHtml);
			})
		}
	});

	$("#delDept1").click(function() {
		if ($("#selDepts1 option:selected").length > 0) {
			$("#selDepts1 option:selected").each(function() {
				$(this).remove();
			})
		}
	});
	
	$("#deptType1").change(function() {
		var dtId = $("#deptType1 option:selected").val();
		$.ajax({
					type : "post",
		  			data:{"udid":dtId},
		 			url: "/ag/lwc/nextLevel_FindfDeptAction.action",
		  			success:function(data){
		  				$("#subDepts1").empty();
	  				$.each(data.depts,function(i,n){
	  					$("#subDepts1").append("<option value="+n.udId+">"+n.udName+"</option>");
					});
		  			}
		});
	});
});


function dept_check(udId,udName,ckBox_id_dept){//注册点击CHECK BOX 的事件。
	
    var j_ckBox_id_dept = "#"+$.trim(ckBox_id_dept);

		var strHtml="";
$(j_ckBox_id_dept).click(function() {
		if($(j_ckBox_id_dept).attr("checked")==true){
		        if(sign=="1"){}
				else{
				if($("#selDepts1 option").length == 1){
					alert("只能选择一个部门！");
					$(j_ckBox_id_dept).attr("checked",false);
					return ;
				}}
		        
		     if ($("#selDepts1 option").length > 0 ) {
					var t = true;
					$("#selDepts1 option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					});
					if(t){
						strHtml="<option class='class1' name='selDepts1' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					strHtml="<option class='class1' name='selDepts1' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				$("#selDepts1").append(strHtml);
			}
			else{//取消打钩，清楚右边的框
//				$("#selDepts1").empty();
						$("#selDepts1 option").each(function(){
						if(udId == $(this).val()){ 
							$(this).remove();
						}
					});
			}
		});
}

function chgIcon(spanId){
	var span_id = "#" + $.trim(spanId);
	var imgsrc= $(span_id).children("img").attr("src");
	if(imgsrc=="/ag/wzp/images/jiahao.gif") {
		$(span_id).children("img").attr("src","/ag/wzp/images/jianhao.gif");
	}else{
		$(span_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");}
}

function findAllDeptType2(){
	var ckBoxId_dept;
	var extern = 0;
	$.ajax({     //ajax
		type: "post",
		url: "/ag/admin/userDept_findAllDeptType.action",
		success:function(data){
		$("#deptul2").empty();
		$.each(data.deptTypes,function(i,n){
//			alert(n.dtName);
		 extern++; 
		 var temp = "span_id_dup"+extern;
		var temp2 = "ul_id_dup"+extern;
		if(n.dtId=="0001"||n.dtId=="0003"||n.dtId=="0002"){}
		else{
		$("#deptul2").append("<li>"+"<span style=\"cursor: pointer;\" id="+temp+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+n.dtName+"</span>"+"<ul style=\"list-style:none\" id="+temp2+">"+"</ul>"+"</li>");
		}
		var span_id = "#" + $.trim(temp);
		var ul_id = "#" + $.trim(temp2);
		$(span_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
		$(span_id).click(function() {
			chgIcon(temp);
			var deptTypeId = n.dtId;
//			alert(deptTypeId);
			$.ajax({     //ajax
	  			type: "post",
	  			data:{"deptTypeId":deptTypeId},
	 			url: "/ag/lwc/findFirstLevel_FindfDeptAction.action",
	  			success:function(data){
	  				if($(ul_id).text()!=""){
		  				$(ul_id).empty();
		  				$("#selDepts1").empty();
		  			}else{
//		  			     alert(data.depts);
		  			     if(data.users!=null){
//		  			     alert(data.users);
		  			   //  setPeople(data.users,temp2);//tmpe2为ul 的id
		  			     }
		  				$.each(data.depts,function(i,n){
		  					extern++;
		  				var temp3 = "span2_id_dup"+extern;
  				     	var temp4 = "ul2_id_dup"+extern;
  				     	ckBoxId_dept = "ckBox_dept"+extern;
	  					$(ul_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp3+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+"</span>"+"<input type=\"checkbox\" id=\""+ckBoxId_dept+"\"/>"+n.udName+"<ul style=\"list-style:none\" id="+temp4+">"+"</ul>"+"</li>");
	  					dept_check(n.udId,n.udName,ckBoxId_dept);
	  					var span2_id = "#" + $.trim(temp3);
	  					var ul2_id = "#" + $.trim(temp4);
	  					$(span2_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
	  					$(span2_id).click(function() {
	  						chgIcon(temp3);
	  						var udid_1 = n.udId;
	  						$.ajax({
	  							type : "post",
	  				  			data:{"udid":udid_1},
	  				 			url: "/ag/lwc/nextLevel_FindfDeptAction.action",
	  				  			success:function(data){
	  				  			if($(ul2_id).text()!=""){
	  				  				$(ul2_id).empty();
//	  				  				$("#selDepts1").empty();
	  				  				$.each(data.depts,function(i,n){
	  				  				$("#selDepts1 option").each(function(){
										if(n.udId == $(this).val()){ 
										$(this).remove();
										}
									});
	  				  				}
	  				  				);
	  				  			}else{
//	  				  			alert(data.depts);
	  				  		if(data.users!=null){
//	  			  			     alert(data.users);
	  			  			//  setPeople(data.users,temp4);//tmpe2为ul 的id
	  				  		}
	  				  		$.each(data.depts,function(i,n){
	  				  			extern++;
	  				  		var temp5 = "span3_id_dup"+extern;
	  				     	var temp6 = "ul3_id_dup"+extern;
	  				     	ckBoxId_dept = "ckBox_dept"+extern;
	  				     	$(ul2_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp5+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+"</span>"+"<input type=\"checkbox\" id=\""+ckBoxId_dept+"\"/>"+n.udName+"<ul style=\"list-style:none\" id="+temp6+">"+"</ul>"+"</li>");
	  				     	dept_check(n.udId,n.udName,ckBoxId_dept);
	  				     	var span3_id = "#" + $.trim(temp5);
		  					var ul3_id = "#" + $.trim(temp6);
		  					$(span3_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
		  					$(span3_id).click(function() {
		  						chgIcon(temp5);
		  					
		  						var udid_2 = n.udId;
		  						$.ajax({
		  							type : "post",
		  				  			data:{"udid":udid_2},
		  				 			url: "/ag/lwc/nextLevel_FindfDeptAction.action",
		  				  			success:function(data){
		  				  			if($(ul3_id).text()!=""){
		  				  				$(ul3_id).empty();
		  				  				$("#selDepts1").empty();
		  				  			}else{
		  				  				if(data.depts!=null){
//		  				  			alert(data.depts);
		  				  				}
		  				  		if(data.users!=null){
//		  			  			     alert(data.users);
		  			  			//  setPeople(data.users,temp6);//tmpe6为ul 的id
		  				  		}
		  				  			}
		  				  			}
		  				  			});
		  						
		  					});
	  				  		});
	  				  				
	  				  			}
	  				  				
	  				  				
	  				  			}
	  							
	  						});

	  						
	  					});
		  					
		  				});
		  				}
			}
		});
			
			
		});
		
		});
		
	}
	});
	
}


